using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

namespace MTRC.ODMS.Web.SOM.CodeSetup
{
    /// <summary>
    /// author:Weiyudong
    /// </summary>
    public partial class FireLinkList : BasePage_SOM
    {
        #region MenuID

        protected override string MenuID
        {
            get
            {
                return "M140811";
            }
        }

        #endregion

        #region Page Init And Load

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.ChangeLanguage += new ChangeLanguageEventHandler(FireLinkList_ChangeLanguage);
            this.grdSearchResult.NeedDataBind += new APJ.Web.UI.WebControls.CommonGridView.NeedDataBindHandler(grdSearchResult_NeedDataBind);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // add by dylan 20151030 for u275.1 
                this.grdSearchResult.PageSize = this.GridViewPageDefaultSize();
                SetLanguage();
                InitPage();
                BindData();
            }
        }

        private void InitPage()
        {
            
            //check user page rights
            //choose none of the item

            if (DAL.Security.User.CanFunctionItemAccess("M140811_F1", this.ODMUser.UserProfileID, this.MenuID))//access all station.
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, true, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
                this.ddlStation.Enabled = true;
                this.ddlStation.SelectedValue = "0";
            }
            else if (DAL.Security.User.CanFunctionItemAccess("M140811_F3", this.ODMUser.UserProfileID, this.MenuID))//Can Access other stations.
            {
                //BindStation
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
                this.ddlStation.Enabled = true;
                this.ddlStation.SelectedValue = "0";
            }
            else 
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), true);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), true);
                this.ddlStation.Enabled = false;
            }

            //update by gordon 20151020 U247
            //if (ddlStation.Items.FindByValue(this.ODMUser.StationID.ToString()) != null)//bind defualt value when ddlstation disabled
            //{
            //    this.ddlStation.SelectedValue = this.ODMUser.StationID.ToString();
            //}
            if (ddlStation.Items.FindByValue(this.ODMUser.LocationID.ToString()) != null)//bind defualt value when ddlstation disabled
            {
                this.ddlStation.SelectedValue = this.ODMUser.LocationID.ToString();
            }

            if (DAL.Security.User.CanFunctionItemAccess("M140811_F2", this.ODMUser.UserProfileID, this.MenuID))
            {
                //update by gordon 20151020 U247
                //BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.StationID), false);
                BindListControl(this.ddlStation, "LocationID", "station", DAL.SOM.CodeSetup.CDAssetType.AccessStationWithUserID(this.ODMUser.ID, false, Convert.ToInt16(LangCode), this.ODMUser.LocationID), false);
                this.ddlStation.Enabled = false;
                this.btnAddNew.Enabled = false;

                //update by gordon 20151020 U247
                //if (ddlStation.Items.FindByValue(this.ODMUser.StationID.ToString()) != null)
                //{
                //    this.ddlStation.SelectedValue = this.ODMUser.StationID.ToString();
                //}
                if (ddlStation.Items.FindByValue(this.ODMUser.LocationID.ToString()) != null)
                {
                    this.ddlStation.SelectedValue = this.ODMUser.LocationID.ToString();
                }
            }
            


            
        }
        /// <summary>
        /// bind data to datagrid
        /// </summary>
        private void BindData()
        {
            this.grdSearchResult.GetLabelFunction = new APJ.Web.UI.WebControls.GetLabel(this.GetLabel);
            //from asset list
            string stationIDs = "";
            this.SetStationSelectedItem(ref stationIDs, this.ddlStation);

            DataTable Table = MTRC.ODMS.DAL.SOM.CodeSetup.CDFireLink.GetFireLinkList(stationIDs, this.txtFireLinkCode.Text, this.chkObsolete.Checked, this.LangCode);
            //this.FormatDateTime(Eval("EffectiveFrom").ToString(), false); 
            //this.FormatDateTime(Eval("EffectiveTo").ToString(), false); 

            DataView View = Table.DefaultView;
            View.Sort = this.grdSearchResult.DataViewSortExpression;

            this.grdSearchResult.DataSource = View;
            this.grdSearchResult.DataBind();

            Table.Dispose();
            Table = null;
        }

       #endregion

        #region SetLanguage

        void FireLinkList_ChangeLanguage(object sender, EventArgs e)
        {
            SetLanguage();
            InitPage();
            BindData();
        }
        /// <summary>
        /// form multi language
        /// </summary>
        private void SetLanguage()
        {
            //this.lblCodeSetupTitle.Text = this.GetLabel("SOM_FireLinkListTitle");
            this.lblStation.Text = GetLabel("SOM_Station", true);
            this.lblFireLink.Text = GetLabel("SOM_FireLinkCode", true);
            this.lblObsolete.Text = GetLabel("COM_Show", true);
            this.chkObsolete.Text = this.GetLabel("SOM_Voided");
            this.btnSearch.Text = GetLabel("COM_Search");
            this.btnAddNew.Text = GetLabel("SOM_AddNewAssetLoanItem");
        }
        
        #endregion

        #region Event
        /// <summary>
        /// for pager funciton
        /// </summary>
        protected void grdSearchResult_NeedDataBind()
        {
            BindData();
        }

        /// <summary>
        /// search click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //BindGrid
            BindData();
        }
        /// <summary>
        /// add a new firelink
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            this.Response.Redirect("FireLinkEntry.aspx?action=new");
        }
        /// <summary>
        /// deal the edit event of gridview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdSearchResult_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToUpper() == ("edit").ToUpper())
            {
                this.Response.Redirect("FireLinkEntry.aspx?action=edit&FireLinkID=" + e.CommandArgument.ToString());
            }
        }
        #endregion

    }
}
